package com.alphacsp.theedge2012.network;

import android.util.Log;
import com.alphacsp.theedge2012.data.entities.*;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.converters.ConverterRegistry;
import com.thoughtworks.xstream.core.JVM;
import com.thoughtworks.xstream.io.HierarchicalStreamDriver;
import com.thoughtworks.xstream.io.xml.XppDriver;
import com.thoughtworks.xstream.mapper.AnnotationMapper;

import java.io.InputStream;

/**
 * Created by IntelliJ IDEA.
 * User: Gilad
 * Date: 20/10/10
 * Time: 15:26
 */
public class XMLParser {

    private static XStream xStream;

    static {
        HierarchicalStreamDriver driver = new XppDriver();
        xStream = new XStream(driver);

        JVM jvm = new JVM();
        AnnotationMapper annotationMapper = new AnnotationMapper(xStream.getMapper(), (ConverterRegistry) xStream.getConverterLookup(),xStream.getConverterLookup(),xStream.getClassLoader(),xStream.getReflectionProvider(),jvm);

        xStream = new XStream(xStream.getReflectionProvider(), driver, xStream.getClassLoader(), annotationMapper);
        xStream.processAnnotations(new Class[]{Speaker.class, Speakers.class, Sessions.class, Session.class, Schedule.class, Event.class});
    }

    @SuppressWarnings({"unchecked"})
    public static <T> T parseXML(InputStream inputStream) {
        Log.i("XMLParser", "Parsing XML");
        try {
            return (T) xStream.fromXML(inputStream);
        } catch (Throwable t) {
            return null;
        }
    }
}
